package com.luckyframe.project.jmeter.jmeterTestPlan;

import com.luckyframe.project.jmeter.jmeterTestCase.domain.JmeterTestCase;
import kg.apc.jmeter.threads.SteppingThreadGroup;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.HashTreeTraverser;

public class CustomJmxConfigTraverser implements HashTreeTraverser {

    private JmeterTestCase testCase;

    public CustomJmxConfigTraverser(JmeterTestCase testCase) {
        this.testCase = testCase;
    }

    @Override
    public void addNode(Object node, HashTree subTree) {
        if(node instanceof  kg.apc.jmeter.threads.SteppingThreadGroup){
            SteppingThreadGroup steppingThreadGroup = (SteppingThreadGroup) node;
            steppingThreadGroup.setThreadGroupDelay(number2String(testCase.getThreadsDelay()));
            steppingThreadGroup.setInUserPeriod(number2String(testCase.getStartUserPeriod()));
            steppingThreadGroup.setInUserCount(number2String(testCase.getStartUserCount()));
            steppingThreadGroup.setInUserCountBurst(number2String(testCase.getStartUserBurst()));
            steppingThreadGroup.setFlightTime(number2String(testCase.getFlightTime()));
            steppingThreadGroup.setOutUserPeriod(number2String(testCase.getStopUserPeriod()));
            steppingThreadGroup.setOutUserCount(number2String(testCase.getStopUserCount()));
            steppingThreadGroup.setRampUp(number2String(testCase.getRampUp()));
            steppingThreadGroup.setNumThreads(number2String(testCase.getThreadsNum()));
        }
    }


    private String number2String(Integer number){
        if(number==null){
            return "0";
        }

        return number.toString();
    }


    @Override
    public void subtractNode() {

    }

    @Override
    public void processPath() {

    }
}
